package com.example.recyclerview.utli;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;

import java.util.regex.Pattern;

/**
 * 不能输入空格;不能输入汉字
 */
public class ChineseCharactersFilter implements InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        //不能输入汉字
        if (checkHZ(source)) {
            return "";
        } else {
            return source;
        }
    }

    /**
     * 汉字正则表达式
     * 返回true为汉字
     */
    private boolean checkHZ(CharSequence str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[\\u4e00-\\u9fa5]*$");
        return pattern.matcher(str).matches();
    }
}
